<?php

/**
 * Backend response for adUnit request
 * getAd.php
 *
 * @package adServer
 */
/*	Load the configuration file	*/
require_once ('config.php');

/*	Load the classes	*/
require_once (CLASS_PATH . 'adServer.class.php');
require_once (CLASS_PATH . 'template.class.php');

/*	Instantiate the session class	*/
$adServer = new adServer();

/* Load the request parameters */
$outputFormat = isset($_REQUEST['format']) ? strtolower($_REQUEST['format']) :
    'jsonp';
$jsonCallback = isset($_REQUEST['jsonp']) && strlen($_REQUEST['jsonp']) > 0 ? $_REQUEST['jsonp'] : null;
$adType = isset($_REQUEST['type']) && strlen($_REQUEST['type']) > 0 ? $_REQUEST['type'] : null;
$adLanguage = isset($_REQUEST['adLanguage']) && strlen($_REQUEST['adLanguage']) >
    0 ? $_REQUEST['adLanguage'] : '--';
$adCampaign = isset($_REQUEST['adCampaign']) && strlen($_REQUEST['adCampaign']) >
    0 ? $_REQUEST['adCampaign'] : 'Default';


/* Determine the output format */
if (isset($adType)) {
    $adUnit = $adServer->getNextAdUnit($adType, $adLanguage, $adCampaign);
    switch ($outputFormat) {
        case 'jsonp':
            jsonpOutput($adUnit);
            break;

        case 'htmladonly':
            htmlAdOnlyOutput($adUnit);
            break;

        default:
            htmlOutput($adUnit);
            break;

    }


} else {
    $header = new template(TEMPLATE_PATH . 'header.tpl');
    $header->replace('&&SIDEMENU&&', '');
    echo $header->templateHtml;
    echo "<span class='txt-error-big-center'>Parameter 'type' is required";
    require_once (TEMPLATE_PATH . 'footer.tpl');
}

/**
 * Output the ad in JSONP format
 * @param object $adUnit
 * @return 
 */
function jsonpOutput($adUnit) {
    global $adServer, $jsonCallback;
    $adUnitJson = array('adUnitId' => $adUnit["adUnitId"], 'adType' => $adServer->
        getTypenameById($adUnit["adTypeId"]), 'adTypeId' => $adUnit["adTypeId"],
        'adDuration' => $adUnit["adDuration"], 'adStartOffsetPercent' => $adUnit["adStartOffsetPercent"],
        'adHtml' => $adUnit["adHtml"]);
    $json = json_encode($adUnitJson);
    header("Content-type: application/json");
    header('Content-Disposition: attachment; filename="adUnit.jsonp"');
    echo $jsonCallback . ' (' . $json . ');';

}

/**
 * Output the ad unit in HTML Format (Also display parameter in a form)
 * @param object $adUnit
 * @return 
 */
function htmlOutput($adUnit) {
    global $adServer;

    /*	Load the header template	*/
    $header = new template(TEMPLATE_PATH . 'header.tpl');
    $header->replace('&&SIDEMENU&&', '');

    /*	Load the page template	*/
    $htmlPage = new template(TEMPLATE_PATH . 'htmldisplay.tpl');
    $htmlPage->replace('&&ADUNITID&&', $adUnit['adUnitId']);
    $htmlPage->replace('&&ADTYPE&&', $adServer->getTypenameById($adUnit["adTypeId"]));
    $htmlPage->replace('&&ADDURATIONVAL&&', $adUnit["adDuration"]);
    $htmlPage->replace('&&ADSTART&&', $adUnit['adStartOffsetPercent']);
    $htmlPage->replace('&&ADCODE&&', $adUnit['adHtml']);

    /* Print out the page */
    echo $header->templateHtml;
    echo $htmlPage->templateHtml;
    require_once (TEMPLATE_PATH . 'footer.tpl');
}

/**
 * Output the ad unit in HTML Format (Only display the adHtml, no parameters)
 * @param object $adUnit
 * @return 
 */
function htmlAdOnlyOutput($adUnit) {
    echo $adUnit['adHtml'];
}
